package com.lmx.wuzhangshao.base;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import com.lmx.wuzhangshao.MyApplication;
import com.lmx.wuzhangshao.R;

import org.w3c.dom.Text;

/**
 * @author Leung
 * @date 2020/7/31 17:18
 */
public abstract class BaseActivity extends AppCompatActivity {

    public TextView tvToolbarLeft;
    public TextView tvToolbarTitle;
    public ImageButton imgBtnToolbarRight;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(setLayoutResId());
        if (isEnableToolBar()) {
            initTitleBar();
        }
        initView();
    }

    /**
     * 设置布局资源id
     *
     * @return 布局资源id
     */
    protected abstract int setLayoutResId();

    /**
     * 是否启用工具栏
     * @return 是否
     */
    public abstract boolean isEnableToolBar();

    /**
     * 初始化标题栏
     */
    private void initTitleBar() {
        tvToolbarLeft = findViewById(R.id.tv_toolbar_left);
        tvToolbarTitle = findViewById(R.id.tv_toolbar_title);
        imgBtnToolbarRight = findViewById(R.id.imgBtn_toolbar_right);

        // 关闭当前界面
        tvToolbarLeft.setOnClickListener((v -> finish()));

        // 设置标题栏的标题
        tvToolbarTitle.setText(setTitleText());
    }

    /**
     * 设置标题文本
     *
     * @return 标题文本
     */
    protected String setTitleText(){
        return "";
    }

    /**
     * 初始化视图
     */
    protected abstract void initView();

    public void setLeftImage(int id) {
        Drawable drawable = ContextCompat.getDrawable(this, id);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        tvToolbarLeft.setCompoundDrawables(drawable, null, null, null);
    }

}
